var id = ""; //栏目id
var userId ="-1" //来源用户id
var shareStatus = "" //分享状态
Page({
  data: { 
    authbg: getApp().globalData.authImg,
    authbtn2: getApp().globalData.source + "red_packet/btn2.png",
    authdialog: getApp().globalData.source + "dialog/dialog.png",
    authbtn: false,
	  myUserId: wx.getStorageSync("userId") == "" ? -1 : wx.getStorageSync("userId")
  },
  authbtn: function () {
    var vm = this
    wx.showLoading({
      title: "正在检查网络.."
    })
    this.setData({
      authbtn: true
    })
  },
  bindGetUserInfo: function (e) {
    getApp().authInfo(e);
  },
  todetails: function (event) {
    var vm = this
    var id = event.currentTarget.dataset.id;
    var index = event.currentTarget.dataset.index;
    var season = event.currentTarget.dataset.season;
    console.log("到底是几" + vm.data.shareList[index].useH5)
    if (vm.data.shareList[index].useH5 == 0) {
      wx.navigateTo({
        url: '../../../../day_day_listen/details/details?id=' + id + "&season=" + season + "&url=" + vm.data.shareList[index].h5Url,
      })
    } else {
      wx.navigateTo({
        url: '../../../../day_day_listen/details/details?id=' + id + "&season=" + season + "&url=" + undefined,
      })
    }
  }, 

  tohome:function(){
    wx.switchTab({
      url: '/pages/style/style',
    })
  },

  onShow:function(){
    if (wx.getStorageSync("userId") == "" || wx.getStorageSync("userId") == undefined) {
      if (wx.getStorageSync("tourist") == "1") {
        if (wx.getStorageSync("authtype") == "0") {
          this.setData({
            showAuth: true,
            showAuth2: false
          })
        } else {
          this.setData({
            showAuth: false,
            showAuth2: true
          })
        }
        return
      }
    } else {
      this.setData({
        showAuth: false,
        showAuth2: false
      })
    }

    this.getProgramDetail(id, userId, this.data.myUserId);
    this.freeShare()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  id = options.id;
	  userId = options.userIds;
	  shareStatus = options.shareStatus; 
	  wx.showLoading({
		  title: "加载中..."
	  })

    if(wx.getStorageSync("userId")==userId){
      this.setData({
        continueshare:true
      })
    }
	  var showhomeicon = options.showhomeicon == "fx" ? true : ""; //判断是分享页进入还是小程序正常进入
	  console.log(showhomeicon)
	  console.log("ddxxx")
	  if (showhomeicon) {
		  this.setData({
			  showhomeicon: showhomeicon
		  })
	  }

    var showhomeicon = options.showhomeicon == "fx" ? true : ""; //判断是分享页进入还是小程序正常进入
    if (showhomeicon) {
      this.setData({
        showhomeicon: showhomeicon
      })
    }
  },



  freeShare:function(){
	  var datas = {
		  "startPoint": 0,
		  "maxPage": 3
	  }
	  var vm = this
	  getApp().requestLoading(getApp().globalData.https + "programResource/programShare",
		  "get", datas, "加载中...",
		  function (res) {
			  if (res.code == 0) {console.log(res)
				  vm.setData({
					  shareList: res.data.data,
				  })
			  }
		  });
  	},
	/**
	 * 	根据栏目id获取栏目详情
	 */
	getProgramDetail:function(id,userId,myUser){
		var that = this;
    console.log("id" + id)
    console.log("srcUserId" + userId)
    console.log("userId" + myUser)
		var data = { programId: id, srcUserId: userId, userId: myUser}
		getApp().requestLoading(getApp().globalData.https + "programResource/shareProgramDetail/" + id + "/" + userId + "/" + myUser,
			"get", data, "加载中...",
			function (res) { 
				if (res.code == 0) {
					var s= []
					s.push(res.data); 
          var newuser_data = []
          if (res.data.shareUsers!=undefined){
            var shareuser = res.data.shareUsers;
            if (shareuser.length > 3) {
              for (var i = 0; i < 3; i++) {
                newuser_data.push(shareuser[i])
              }
            }else{
              newuser_data = shareuser;
            }
          }
					that.setData({
            shareuser: newuser_data,
						programDetail: res.data,
						programDetailList:s
					})
					that.nowTime()
				}
			});
  	},
	  /**
	   * 点击查看更多跳转
	   */
	freeShareBindTap:function(){
		wx.navigateTo({
			url: "../../../day_day_listen/sharelist/sharelist",
			success: function () {       
			},
			fail: function () {
			}
		})

	},
  /**
 * 点击查看更多下边的列表跳转
 */
  freeShareDetailBindtap: function (event) {
    var vm = this
    var id = event.currentTarget.dataset.id;
    var index = event.currentTarget.dataset.index;
    if (vm.data.shareList[index].useH5 == 0) {
      wx.navigateTo({
        url: '../../../day_day_listen/details/details?id=' + id + "&season=" + 1 + "&url=" + vm.data.shareList[index].h5Url,
      })
    } else {
      wx.navigateTo({
        url: '../../../day_day_listen/details/details?id=' + id + "&season=" + 1 + "&url=" + undefined,
      })
    }
   
  },

	/**
	 * 给好友助力
	 * 邀请好友助力
	 */
	helperFirBindtap:function(e){
		console.log("进入给好友助力")
    console.log(this.data.programDetail.shareUsers)
    if (this.data.programDetail.shareUsers=!undefined ){
      if (this.data.programDetail.shareUsers.length>=3){
        wx.showToast({
          title: '对方已完成好友助力了哦',
          icon: "none",
          duration: 1500
        })
        return
      }
    }
		var that  = this;
		if (wx.getStorageSync("userId") == "" || wx.getStorageSync("userId") == undefined) {
			console.log("进入给好友助力-未登录")
			wx.navigateTo({
				url: '../../../login/login',
			})
		}else{
			console.log("进入给好友助力-已登录")
			var userInfo = wx.getStorageSync("userInfo") //当前登录人信息
			var myUserId = wx.getStorageSync("userId") //当前登录人id
      if (userId==myUserId){
        wx.showToast({
          title: '不能给自己助力哦',
          icon: "none",
          duration: 1500
        })
        return;
      }
			var fromUserId = userId; //来源用户id，来自谁分享的栏目
			var data = { srcUserId: fromUserId, programId: id, userId: myUserId, wxAvatarUrl: userInfo.avatarUrl, wxCity: userInfo.city, wxNickname: userInfo.nickName, wxGender: userInfo.gender, wxProvince: userInfo.province }
			console.log(data)
			getApp().requestLoading(getApp().globalData.https + "programResource/shareCheck",
				"post", data, "加载中...",
				function (res) {
					if (res.code == 0) {
						console.log(res)
						that.getProgramDetail(id, fromUserId, myUserId);
					}
				})
		}
	},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    id = options.id;
    userId = options.userIds;
    shareStatus = options.shareStatus; 
    return {
      path: "/pages/my/my_share/sharing_details/sharing_details?&showhomeicon=fx&id=" + id + "&userIds=" + userId + "&shareStatus=" + shareStatus,
      success(res) {
        console.log("success")
      },
      fail(e) {
        console.log("fail")
      },
      //分享完成
      complete() {
        console.log("complete")
      }
    }
  },


	nowTime: function () {
		var that = this;
		var dataList = that.data.programDetailList
		// 获取当前时间，同时得到活动结束时间数组
		let newTime = new Date().getTime();
		for (var i = 0; i < dataList.length; i++) {
			var createtime = dataList[i].shareCreateTime //创建时间 日期
			var endTime = dataList[i].survivalTime;//获取结束时间 小时
			endTime = createtime + endTime
			var day = 0, hour = 0, minute = 0, second = 0;
			if (endTime - newTime > 0) {
				let time = (endTime - newTime) / 1000;
				day = Math.floor(time / (60 * 60 * 24));
				hour = Math.floor(time / (60 * 60)) - (day * 24);
				minute = Math.floor(time / 60) - (day * 24 * 60) - (hour * 60);
				second = Math.floor(time) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
				if (hour <= 9) hour = '0' + hour;
				if (minute <= 9) minute = '0' + minute;
				if (second <= 9) second = '0' + second;
				dataList[i].survivalTime--;
				var str = "还剩" + hour + ':' + minute + ':' + second + "结束" 
				if (shareStatus === "0") {
					str = "已助力成功"
				} else if (shareStatus === "2") {
					str = "助力失败"
				}
			} else {
				var str = "00:00:00";
				//clearInterval(nowTime); 
				if (shareStatus === "0") {
					str = "成功"
				} else if (shareStatus === "1") {
					str = "进行中";
				} else if (shareStatus === "2") {
					str = "失败"
				}
			} 
			dataList[i].timers = str;//将timers时间字段放入数组中   
		}
		that.setData({
			programDetail: dataList[0],
			programDetailList: dataList
		})
		setTimeout(that.nowTime, 1000);

	},

	// 回到首页
	tohome: function () {
    console.log("回到首页")
		wx.switchTab({
      url: '/pages/style/style',
		})
	},
})